package com.school.chapter02.tree_;

import java.util.Arrays;

public class BinaryTreeDemo {
    public static void main(String args[]) {
        int[] preOrders = new int[] {1, 2, 4, 8, 5, 3, 6, 7};
        int[] inOrders = new int[] {8, 4, 2, 5, 1, 6, 3, 7};
        BinaryTree binaryTree = new BinaryTree().buildTree(preOrders, inOrders);
        binaryTree.preOrder();

    }
}
 class BinaryTree {
    private TreeNode root;
    public void setRoot(TreeNode root){this.root = root;}

    public void delNode(int no){
        if (root != null){
            if (root.getNo() == no)root = null;
            else root.delNode(no);
        }else System.out.println("空树，不能删除");
    }
    public void preOrder(){
        if (this.root != null)this.root.preOrder();
        else System.out.println("二叉树为空，无法遍历");
    }
    public void infixOrder(){
        if (this.root != null)this.root.infixOrder();
        else System.out.println("二叉树为空，无法遍历");
    }
    public void postOrder(){
        if (this.root != null)this.root.postOrder();
        else System.out.println("二叉树为空，无法遍历");
    }
    static BinaryTree buildTree(int[] preOrders, int[] inOrders) {
        if (preOrders.length == 0 || inOrders.length == 0) {
            return null;
        }
        BinaryTree tree = new BinaryTree();
        tree.root = new TreeNode(1,preOrders[0]);
        int index = search(0, inOrders.length, inOrders, tree.root.getName());
        tree.root.setLeft(buildTree(Arrays.copyOfRange(preOrders, 1, index + 1), Arrays.copyOfRange(inOrders, 0, index)).root);
        tree.root.setRight(buildTree(Arrays.copyOfRange(preOrders, index + 1, preOrders.length),
                Arrays.copyOfRange(inOrders, index + 1, inOrders.length)).root);
        return tree;
    }
    private static int search(int start, int end, int[] inOrders, int data) {
        for (int i = start; i < end; i++) {
            if (data == inOrders[i]) {
                return i;
            }
        }
        return -1;
    }
}

class TreeNode{
    private int no;
    private int name;
    private TreeNode left;
    private TreeNode right;

    public TreeNode(int no, int name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public int getName() {
        return name;
    }

    public void setName(int name) {
        this.name = name;
    }

    public TreeNode getLeft() {
        return left;
    }

    public void setLeft(TreeNode left) {
        this.left = left;
    }

    public TreeNode getRight() {
        return right;
    }

    public void setRight(TreeNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
    public void delNode(int no){
        if (this.left != null && this.left.no == no){
            this.left = null;
            return;
        }
        if(this.right != null && this.right.no == no){
            this.right = null;
            return;
        }
        if (this.left != null){
            this.left.delNode(no);
        }
        if(this.right != null){
            this.right.delNode(no);
        }
    }
    //前序遍历
    public void preOrder(){
        System.out.println(this);
        if (this.left != null)this.left.preOrder();
        if (this.right != null)this.right.preOrder();
    }
    //中序遍历
    public void infixOrder(){
        if (this.left != null)this.left.infixOrder();
        System.out.println(this);
        if (this.right != null)this.right.infixOrder();
    }
    //后序遍历
    public void postOrder(){
        if (this.left != null)this.left.postOrder();
        if (this.right != null)this.right.postOrder();
        System.out.println(this);

    }
}